package org.wsalger.zm.zmbus;

import org.wsalger.soap.ClsFunction;
import org.wsalger.task.WebServiceAsyncTask;
import org.wsalger.tools.ClsTools;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
	private Button butlogin = null;
	private EditText username = null;
	private EditText userpass = null;
	private EditText txtservice=null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		butlogin = (Button) findViewById(R.id.butlogin);
		username = (EditText) findViewById(R.id.username);
		userpass = (EditText) findViewById(R.id.userpass);
		
		butlogin.setOnClickListener(l);
		
		checkInternet();
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		if (item.getItemId() == 1) {
			AlertDialog.Builder build=new AlertDialog.Builder(this);
			//Toast.makeText(MainActivity.this, "A", Toast.LENGTH_LONG).show();
			Context ctx = MainActivity.this;
			SharedPreferences sp = ctx.getSharedPreferences("SET", MODE_PRIVATE);
			LayoutInflater factory=LayoutInflater.from(this);
			final View textView=factory.inflate(R.layout.custom_set, null);
			txtservice = (EditText) textView.findViewById(R.id.txtService);
			txtservice.setText(sp.getString("SERVER_SET", "127.0.0.1:8080"));
			build.setTitle("服务器设置");
			build.setView(textView);
			build.setPositiveButton("确定",new android.content.DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					Context ctx = MainActivity.this;
					SharedPreferences sp = ctx.getSharedPreferences("SET", MODE_PRIVATE);
					Editor editor = sp.edit();
					editor.putString("SERVER_SET", txtservice.getText().toString());
					editor.commit();
					checkInternet();
				}
			});
			build.setNegativeButton("取消",new android.content.DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					dialog.dismiss();
				}
			});
			build.setNeutralButton("测试连接",new android.content.DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					checkInternet();
					//Toast.makeText(MainActivity.this,"A" , Toast.LENGTH_LONG).show();
				}
			});
			build.create();
			build.show();
		}
		return true;// super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		menu.add(0, 1, 1, "服务器设置");
		return true;// super.onCreateOptionsMenu(menu);
	}
		
	//登录按钮
	private OnClickListener l = new OnClickListener() {

		@Override
		public void onClick(View v) {
			String keyStr;
			TelephonyManager tm = (TelephonyManager) getBaseContext()
					.getSystemService(Context.TELEPHONY_SERVICE);
			// TODO Auto-generated method stub
			keyStr = ClsFunction.login(
					ClsTools.getMD5(username.getText().toString()),
					ClsTools.getMD5(userpass.getText().toString()),
					tm.getDeviceId());
			Intent i = new Intent();
			i.setClass(MainActivity.this, MainInActivity.class);
			i.putExtra("keyStr", keyStr);
			startActivity(i);
		}
	};
	
	//页面加载时检测webservice的情况
	public void checkInternet(){
		String endpoint=null;
		Context ctx = MainActivity.this;
		SharedPreferences sp = ctx.getSharedPreferences("SET", MODE_PRIVATE);
		endpoint="http://"+sp.getString("SERVER_SET", "127.0.0.1:8080")+"/zmbusiness/webservice/zm_webservice";
		
		WebServiceAsyncTask task=new WebServiceAsyncTask();
		task.execute(endpoint,"f_getSystemDate");
	}
}
